﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

using h = functionService.shorts;
using da = functionService.DATECOD;
using fr = HK.FiscalRegistrar.FiscalRegistrar;
using vv = functionService.serfun_city7;
using tgw = functionService.tagWork;
using HK.Reports;


namespace ETALON
{
   
  
    
    
    
    public partial class FormDoc : Form
    {
        const int i_NKP = 1, i_SFP = 2, i_SFR = 3, i_NKR = 4;
        functionService.prm_city q; raw.modeWAED currentModeWAED; REPORTS.PRINT prnt; raw.typeDoc currentTypeDoc;
        DataTable rd, td, rdDod;
        
        
        
        public FormDoc() { InitializeComponent(); InitializeComponentMy();  }
        public FormDoc(int indexDoc) : this() { selectOnIndex(indexDoc); setModeWAED(ETALON.raw.modeWAED.loadForReadOnly);}
        private void InitializeComponentMy()
        {
            rd = new DataTable(); td = new DataTable();
            q = new functionService.prm_city(); 
            q.con.pass =  q.tk(param.pass_inServ_55); q.con.co(q.tk(param.server_1));
            toolStripStatusLabel1.Text = q.tk(param.server_1);
            string sprset = q.tk((int)param.pr_tov_inNk2_question_51);//0-no,1-yes
            toolStripMenuItem1.Checked = fatr(sprset);
  

            currentModeWAED = ETALON.raw.modeWAED.load;
            setModeTypeDoc(ETALON.raw.typeDoc.NKP);
            this.Text = functionService.shorts.a( "Рабочий день: ", q.tk(param.day_work_7));
            prnt = new ETALON.REPORTS.PRINT();  
            loadDoc();


        }
        private bool true_false(int i1i0) { bool b = false; if (i1i0 == 0) { b = false; } else if (i1i0==1){ b = true; } return b; }
        private bool fatr(string s1s0) { bool b = false; if (s1s0 == "0") { b = false; } else if (s1s0 == "1") { b = true; } return b; }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
           
        }
        private void loadDocTd()
        {

            listView2.Items.Clear();
            /*
             * SELECT name_cod(n.name, ct) as name, kol, pr, kol*pr as sd ,codd, s.cod123
             * FROM sir_td s left join listtov n on s.ct=n.cod 
             * where codd = coddo 
             * order by s.cod123;
             * 
             */
            object iCodd = q.tk_readTagRd(param.sir_tag_rd_24, ETALON.raw.typeTagRd.codd_5);
           // h.m(22);
            //--------------------------------------
            td = q.selectAny(sql_doc.td(iCodd));
            //--------------------------------------
            //q.sh(td);
           // h.m(23);
            object osd = q.selectScalar(sql_doc.td_sum(iCodd));
            this.listView2.Columns[4].Text = functionService.shorts.a("Сумма:", osd);
            ListViewItem lvi; ListViewItem.ListViewSubItem lvis;
            foreach (DataRow r in td.Rows)
            {
                lvi = new ListViewItem();
                lvi.Text = r[0].ToString(); //r[0] - name
               
                lvis = new ListViewItem.ListViewSubItem();
                lvis.Text = r[1].ToString(); //kol1
                lvi.SubItems.Add(lvis);

                lvis = new ListViewItem.ListViewSubItem();
                lvis.Text = r[2].ToString(); //kol2
                lvi.SubItems.Add(lvis);


                lvis = new ListViewItem.ListViewSubItem();
                lvis.Text = r[3].ToString();//pr
                lvi.SubItems.Add(lvis);

                lvis = new ListViewItem.ListViewSubItem();
                lvis.Text = r[4].ToString();//sd
                lvi.SubItems.Add(lvis);

                                                  //r[0]-name, r[1]-kol, r[2]-kol2 , r[3]-pr, r[5]-codd, r[6]-cod123 
                lvi.Tag = functionService.shorts.az(r[0],      r[1],     r[2],       r[3],    r[5],      r[6]);
                lvi.ImageIndex = 2;
                this.listView2.Items.Add(lvi);
            }
        }
        private void loadDoc()
        {
            
            listView3.Items.Clear();
            /*
             * SELECT right(codd,3)*1 as codd3, s.nd,name_cod(n.name, s.codo) as codo, s.cod123, s.dd, s.codd
                FROM sir_rd s
                left join sir_listorg n on s.codo = n.cod
                where
                (1*mid(codd,6,1)=typeDoc)
                and
                (left(codd,5)*1 = coddDate_1year_09month_13day_10913) order by codd;
             * 
             */
            string sdd = q.tk(param.day_work_7);
            rd = q.selectAny(sql_doc.rdDocType((int)currentTypeDoc, da.codDATE_10913_from_DATETIME(sdd)));
            ListViewItem lvi;
            foreach (DataRow r in rd.Rows)
            {
                lvi = new ListViewItem();
                lvi.Text = functionService.shorts.a("№", r[0], "/", r[1]," ", functionService.DATECOD.cod_to_date(r[4])," ", r[2]); //r[3] - cod123 r[4] - coddate
                // enum typeTagTd { name_0 = 0, kol_1 = 1, kol2_2 = 2, pr_3 = 3, codd_5 = 5, cod123_6 = 6 }
                // enum typeTagRd { codd001_0=0, cod123_1=1, dd_2=2, corg_3=3, nd_4=4, codd_5=5 }
                lvi.Tag = functionService.shorts.az( r[0], r[1], r[2], r[3], r[4], r[5]);
              
                
                //r[0]-name, r[1]-kol, r[2]-kol2 , r[3]-pr, r[5]-codd, r[6]-cod123 
               // lvi.Tag = functionService.shorts.az(r[0], r[1], r[2], r[3], r[5], r[6]);
                
               
                lvi.ImageIndex = 10;
                listView3.Items.Add(lvi);
            }
            setModeWAED(ETALON.raw.modeWAED.load);
        }
        
        private int findInListView(string scoddo)
        {
            int ic = listView3.Items.Count; string codd_tag5 = ""; int ifi=0;
            for (int i = 0; i < ic; i++)
            {
                codd_tag5 = functionService.tagWork.readTAG( listView3.Items[i].Tag.ToString(), 5);
                if (codd_tag5 == scoddo) { ifi = i; break; }
            }
            return ifi;
        }
        private void addDocTd_SkladNk2()
        {
            string s = "", pr = "0", kol = "", kol2 = "";
            listAnyEdit f = new listAnyEdit("city", vv.list_sourse, false);
            f.ShowDialog(); string s51 = q.tk((int)param.pr_tov_inNk2_question_51); 
            
            if (fatr(s51))
            {
             FormKolZakaz fk = new FormKolZakaz("Введите ЦЕНУ"); fk.ShowDialog();  if ( fk.IsSelect)  { pr = fk.valueEnter; }
            }
             FormKolZakaz fkk = new FormKolZakaz("Введите КОЛИЧЕСТВО","1"); fkk.ShowDialog();
             FormKolZakaz f2 = new FormKolZakaz("Введите КОЛИЧЕСТВО 2"); f2.ShowDialog();

            if (f.IsOK && fkk.IsSelect && f2.IsSelect)
            {
                object ocod123 = q.selectScalar("select max(cod123)+1 as cod123 from sir_td;");
                kol = fkk.valueEnter;
                kol2 = f2.valueEnter;
                pr = q.tk((int)param.pr_tov_inNk2_sklad_50);// fk.valueEnter;
                s = sql_doc.ADD_td(ocod123, f.sCod, pr, kol, kol2, q.tk_readTagRd(param.sir_tag_rd_24, ETALON.raw.typeTagRd.codd_5));
                q.UpdateData(s);
            }
            setModeWAED(ETALON.raw.modeWAED.addTd);
            loadDocTd();
        }
        private void addDocTd()
        {
            //rdDod = q.selectAny(sql_doc.rdDoc((int)currentTypeDoc, q.codWorkDAY_10913)); //q.sh(rdDod);
            //int ic = functionService.nextCod.codNext2(rdDod), icc = 1000 + ic; string iccs = icc.ToString().Substring(1, 3); //fs.shorts.m(ic);
            
            //string sicd = string.Concat(q.codWorkDAY_10913, (int)currentTypeDoc, iccs);
            //int icodd = Convert.ToInt32(sicd);
            //string sql_dml_add = sql_doc.ADD_rd(icodd, corgo, functionService.DATECOD.get_Now_YearMonthDate, q.tc_int(param.o_l_0), ""); //fs.shorts.m(sql_dml_add);
            //q.UpdateData(sql_dml_add);
            //loadDoc();
                                //int iCodd = Convert.ToInt32( q.tk_readTag(param.sir_tag_rd_24, ETALON.raw.typeTag.codd_5));
                                //td = q.selectAny(sql_doc.td(iCodd));

            listAnyEdit f = new listAnyEdit("city", vv.list_sourse, true); f.ShowDialog(); 
           // FormKolZakaz fk = new FormKolZakaz("Введите ЦЕНУ"); fk.ShowDialog(); 
            FormKolZakaz fkk = new FormKolZakaz("Введите КОЛИЧЕСТВО"); fkk.ShowDialog();
            FormKolZakaz f2 = new FormKolZakaz("Введите КОЛИЧЕСТВО 2"); f2.ShowDialog();
            // int j = Convert.ToInt32(ocod123);
            string s = "", pr = "", kol = "", kol2 = "1";
            if (f.IsOK && fkk.IsSelect)// if (f.IsOK && fk.IsSelect && fkk.IsSelect)
            {
                object ocod123 = q.selectScalar("select max(cod123)+1 as cod123 from sir_td;");
                kol = fkk.valueEnter; 
                kol2 = f2.valueEnter;
                pr = "0";
                s = sql_doc.ADD_td(ocod123, f.sCod, pr, kol, kol2, q.tk_readTagRd(param.sir_tag_rd_24, ETALON.raw.typeTagRd.codd_5)); 
                q.UpdateData(s);
            }
            setModeWAED(ETALON.raw.modeWAED.addTd );
            loadDocTd();
        }
        private void addDoc( int corgo)
        {
            string sdd = q.tk(param.day_work_7);
            int icd = da.codDATE_10913_from_DATETIME(sdd);
            object occ = q.selectScalar(string.Concat("SELECT COUNT(codd) as codd FROM sir_rd WHERE 1*left(codd,5) = '", icd, "' ;"));
            //h.m(occ);
            int ic = 0, icc = 1000 + ic; string iccs = ""; //fs.shorts.m(ic);
            string sicd = string.Concat(icd, (int)currentTypeDoc, "001");
            int icodd = 0;


            if (Convert.ToInt32(occ) > 0)
            {
                rdDod = q.selectAny(sql_doc.rdDoc((int)currentTypeDoc, icd)); //q.sh(rdDod);

                ic = functionService.nextCod.codNext2(rdDod); icc = 1000 + ic; iccs = icc.ToString().Substring(1, 3);// h.m(h.az(ic, "=ic"));
                sicd = string.Concat(icd , (int)currentTypeDoc, iccs);
                icodd = Convert.ToInt32(sicd);
            }
            else
            {            //112161002
                icodd = icd * 10000 +  1000*(int)currentTypeDoc  + 1; //h.m(h.az(icodd,"=icodd"));
            }
            object ocod123New = q.selectScalar("select max(cod123)+1 as cod123 from sir_rd;");
            
            string sql_dml_add = sql_doc.ADD_rd
                (
                Convert.ToInt32(ocod123New),
                icodd,
                corgo, 
                functionService.DATECOD.get_Now_YearMonthDate,
                vv.o_l_0_cod ,  //q.tc_int(param.o_l_0)
                "0"
                );
            //h.m(sql_dml_add);
            q.UpdateData(sql_dml_add); 
            loadDoc();
        }
        private void delDoc()
        {
            string s = listView3.SelectedItems[0].Tag.ToString(); h.m(s);
            int cod123 = functionService.tagWork.readTAGi(s, (int)raw.typeTagRd.cod123_3); h.m(cod123);
            q.UpdateData(sql_doc.DEL_rd(cod123));
            listView3.SelectedItems[0].Remove();
            
        }
        private void editDoc()
        {
            string s = listView3.SelectedItems[0].Tag.ToString();// h.m(s);
            int cod123 = functionService.tagWork.readTAGi(s, (int)raw.typeTagRd.cod123_3 );
         //  h.m(cod123 );
            editParam_SELECT_sirRd(cod123);

        }
        private void editParam_SELECT_sirRd(int cod123o)
        {

           string tagSelect = "";
            /*codo    dd     codsotr     nd*/

            ListViewItem[] vi = new ListViewItem[3];

            vi[0] = new ListViewItem("№Док");
            vi[1] = new ListViewItem("Дата");
            vi[2] = new ListViewItem("Наименование (код)");

            vi[0].ImageIndex = 1;
            vi[1].ImageIndex = 11;
            vi[2].ImageIndex = 3;

            vi[0].Tag = string.Concat((int)workType.text, "; nd");
            vi[1].Tag = string.Concat((int)workType.date, "; dd");
            vi[2].Tag = string.Concat((int)workType.selectOrg, "; codo");


            //functionService.tagWork.readTAG 
            vi[0].SubItems.Add(q.tk_readTagRd(param.sir_tag_rd_24, ETALON.raw.typeTagRd.nd_1 ));
            string s24 = q.tk(param.sir_tag_rd_24);
            
            //h.m(h.az(1, s24 ));
            vv.editParam_date_25 = 
                functionService.tagWork.readTAG(s24, (int)raw.typeTagRd.dd_4);
           // h.m(vv.editParam_date_25);


            vi[1].SubItems.Add(functionService.DATECOD.cod_to_date(Convert.ToInt32(vv.editParam_date_25)));
           // h.m(h.az(Convert.ToInt32(vv.editParam_date_25), 2));
            vi[2].SubItems.Add(functionService.tagWork.readTAG(q.tk(param.sir_tag_rd_24), (int)raw.typeTagRd.name_cod_corg_2 ));

            vi[0].Font = new Font("Tahoma", 24);
            vi[1].Font = new Font("Tahoma", 24);
            vi[2].Font = new Font("Tahoma", 24);

            vv.sir_org_23 = q.tk((int)param.sir_org_23); 
            FormListParam f = new FormListParam(vi); f.ShowDialog();

            if (f.IsOk)
            {
                string s1 = f.listView1.Items[0].SubItems[1].Text;

                string sql_Update = string.Concat("UPDATE city.sir_rd SET nd = '", s1,
                    "', dd = ", o_editParam_date_25_toCodInt(vv.editParam_date_25), //da.date_to_cod_str( vv.editParam_date_25)
                    ", codo = ", vv.sir_org_23_CodInt,
                    " where cod123 = ", cod123o, ";");
                //h.m(h.aa(sql_Update, "  vv.editParam_date_25=", vv.editParam_date_25));
                if(vv.o_l_0_cod==4) h.m(h.az(sql_Update," only for programmer") );
                
                q.UpdateData(sql_Update);
                object o = q.selectScalar(string.Concat("SELECT listSourse FROM sir_listorg WHERE cod=", vv.sir_org_23_CodInt, ";"));
                vv.list_sourse = o.ToString();
                loadDoc();
            }
        }
        private object o_editParam_date_25_toCodInt(object  sv)
        {
            object o ="";
            if (sv.ToString().Contains("."))
            {
                o = da.date_to_cod_str(sv); h.m(o);
            }
            else
            {
                 o = sv; h.m(o);
            }

            return o;
        }
        private void setStatus(string stxto)
        {
            toolStripStatusLabel3.Text = stxto ;
        }
        #region MODE
        private void setModeWAED(raw.modeWAED mo)
        {


            if (mo == ETALON.raw.modeWAED.load )
            {
                #region Enabled true false
                toolStripMenuItem7.Enabled = false; //печать
                toolStripButton3.Enabled  = true;  //+
                toolStripButton4.Enabled = false;   // удаление док
                toolStripButton6.Enabled  = false;  //редактир
                toolStripButton1.Enabled  = false;  //+ товар
                toolStripButton2.Enabled = false;   //уд товар
                toolStripButton5.Enabled  = false;  // ред товар

                toolStripMenuItem2.Enabled = true; //нк п
                toolStripMenuItem4.Enabled = true;  //сф п
                toolStripMenuItem5.Enabled = true; //сф р
                toolStripMenuItem6.Enabled = true; //нк р

                toolStripMenuItem3.Enabled = true;   //сервис
                #endregion
            }
            else if (mo == ETALON.raw.modeWAED.addRd)
            {
                #region Enabled true false
                toolStripMenuItem7.Enabled = false; //печать
                toolStripButton3.Enabled = true;  //+
                toolStripButton4.Enabled = false;   // удаление док
                toolStripButton6.Enabled = false;  //редактир
                toolStripButton1.Enabled = false;  //+ товар
                toolStripButton2.Enabled = false;   //уд товар
                toolStripButton5.Enabled = false;  // ред товар

                toolStripMenuItem2.Enabled = true; //нк п
                toolStripMenuItem4.Enabled = true;  //сф п
                toolStripMenuItem5.Enabled = true; //сф р
                toolStripMenuItem6.Enabled = true; //нк р

                toolStripMenuItem3.Enabled = true;   //сервис
                #endregion
            }
            else if (mo == ETALON.raw.modeWAED.addTd)
            {
                #region Enabled true false
                toolStripMenuItem7.Enabled = false; //печать
                toolStripButton3.Enabled = false;  //+
                toolStripButton4.Enabled = false;   // удаление док
                toolStripButton6.Enabled = false;  //редактир
                toolStripButton1.Enabled = true;  //+ товар
                toolStripButton2.Enabled = false;   //уд товар
                toolStripButton5.Enabled = false;  // ред товар

                toolStripMenuItem2.Enabled = false; //нк п
                toolStripMenuItem4.Enabled = false;  //сф п
                toolStripMenuItem5.Enabled = false; //сф р
                toolStripMenuItem6.Enabled = false; //нк р

                toolStripMenuItem3.Enabled = false;   //сервис
                #endregion
            }
            else if (mo == ETALON.raw.modeWAED.afterSelectRd)
            {
                #region Enabled true false
                toolStripMenuItem7.Enabled = false; //печать
                toolStripButton3.Enabled = true;  //+
                toolStripButton4.Enabled = true;   // удаление док
                toolStripButton6.Enabled = true;  //редактир
                toolStripButton1.Enabled = true;  //+ товар
                toolStripButton2.Enabled = false;   //уд товар
                toolStripButton5.Enabled = false;  // ред товар

                toolStripMenuItem2.Enabled = true; //нк п
                toolStripMenuItem4.Enabled = true;  //сф п
                toolStripMenuItem5.Enabled = true; //сф р
                toolStripMenuItem6.Enabled = true; //нк р

                toolStripMenuItem3.Enabled = true;   //сервис
                #endregion
            }
            else if (mo == ETALON.raw.modeWAED.afterSelectTd)
            {
                #region Enabled true false
                toolStripMenuItem7.Enabled = false; //печать
                toolStripButton3.Enabled = false;  //+
                toolStripButton4.Enabled = false;   // удаление док
                toolStripButton6.Enabled = false;  //редактир
                toolStripButton1.Enabled = true;  //+ товар
                toolStripButton2.Enabled = true;   //уд товар
                toolStripButton5.Enabled = true;  // ред товар

                toolStripMenuItem2.Enabled = false; //нк п
                toolStripMenuItem4.Enabled = false;  //сф п
                toolStripMenuItem5.Enabled = false; //сф р
                toolStripMenuItem6.Enabled = false; //нк р

                toolStripMenuItem3.Enabled = true;   //сервис
                #endregion
            }
            else if (mo == ETALON.raw.modeWAED.delRd)
            {
                #region Enabled true false
                toolStripMenuItem7.Enabled = false; //печать
                toolStripButton3.Enabled = true;  //+
                toolStripButton4.Enabled = false;   // удаление док
                toolStripButton6.Enabled = false;  //редактир
                toolStripButton1.Enabled = false;  //+ товар
                toolStripButton2.Enabled = false;   //уд товар
                toolStripButton5.Enabled = false;  // ред товар

                toolStripMenuItem2.Enabled = true; //нк п
                toolStripMenuItem4.Enabled = true;  //сф п
                toolStripMenuItem5.Enabled = true; //сф р
                toolStripMenuItem6.Enabled = true; //нк р

                toolStripMenuItem3.Enabled = true;   //сервис
                #endregion
            }
            else if (mo == ETALON.raw.modeWAED.delTd)
            {
                #region Enabled true false
                toolStripMenuItem7.Enabled = false; //печать
                toolStripButton3.Enabled = true;  //+
                toolStripButton4.Enabled = false;   // удаление док
                toolStripButton6.Enabled = false;  //редактир
                toolStripButton1.Enabled = true;  //+ товар
                toolStripButton2.Enabled = false;   //уд товар
                toolStripButton5.Enabled = false;  // ред товар

                toolStripMenuItem2.Enabled = false; //нк п
                toolStripMenuItem4.Enabled = false;  //сф п
                toolStripMenuItem5.Enabled = false; //сф р
                toolStripMenuItem6.Enabled = false; //нк р

                toolStripMenuItem3.Enabled = true;   //сервис
                #endregion
            }
            else if (mo == ETALON.raw.modeWAED.selectRd)
            {

            }
            else if (mo == ETALON.raw.modeWAED.selectTd)
            {

            }
            else if (mo == ETALON.raw.modeWAED.loadForReadOnly)
            {
                #region Enabled true false
                toolStripMenuItem7.Enabled = true; //печать
                toolStripButton3.Enabled = false;  //+
                toolStripButton4.Enabled = false;   // удаление док
                toolStripButton6.Enabled = false;  //редактир
                toolStripButton1.Enabled = false;  //+ товар
                toolStripButton2.Enabled = false;   //уд товар
                toolStripButton5.Enabled = false;  // ред товар

                toolStripMenuItem2.Enabled = false; //нк п
                toolStripMenuItem4.Enabled = false;  //сф п
                toolStripMenuItem5.Enabled = false; //сф р
                toolStripMenuItem6.Enabled = false; //нк р

                toolStripMenuItem3.Enabled = true;   //сервис
                //h.m("loadForReadOnly");
                #endregion
            }
            currentModeWAED = mo;
            
        }
        #endregion

        private void listView3_SelectedIndexChanged(object sender, EventArgs e)
        {
           // h.m(90); listView3_Select(); 
            try { listView3_Select(); }  catch { }
           
        }
        private void listView3_Select()
        {
            //h.m(listView3.SelectedItems.Count );
            int ic = listView3.SelectedItems.Count;
            if (ic > 0)
            {
                string stext = listView3.SelectedItems[0].Text;
                string stag = listView3.SelectedItems[0].Tag.ToString();

                //h.m(h.aa(stext, stag));
                // string stag_doc = functionService.tagWork.readTAG(stag, 0);   
                string scodo =
                 functionService.nameCod.readCod
                 (
                 functionService.tagWork.readTAG(stag, (int)raw.typeTagRd.name_cod_corg_2)
                 );
                object os = q.selectScalar(string.Concat("SELECT listSourse FROM sir_listorg WHERE cod = ", scodo, ";"));
                vv.list_sourse = os.ToString();
                string stag_doc = functionService.tagWork.readTAG(stag, (int)raw.typeTagRd.codd001_0);
                q.put(param.sir_current_doc_20, stag_doc);
                string stag_doc_cod123 = functionService.tagWork.readTAG(stag, (int)raw.typeTagRd.cod123_3);

                // h.m(h.az(stext, "=stext", stag, "=stag", stag_doc, "=stag_doc", stag_doc_cod123, "=stag_doc_cod123"));
                q.put(param.sir_current_doc_cod123_21, stag_doc_cod123); q.put(param.sir_tag_rd_24, stag);
                // fs.shorts.m(stext);
                // h.m(1110);
                setStatus(stext);
                //  h.m(1111);
                //------------------
                loadDocTd();
                //------------------
                // h.m(1112);
                // h.m(currentModeWAED.ToString());
                if (currentModeWAED != ETALON.raw.modeWAED.loadForReadOnly)
                {
                    //h.m(2);
                    setModeWAED(ETALON.raw.modeWAED.afterSelectRd);
                }
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            delDoc();
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {

        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            
        }

        private void toolStripButton5_Click_1(object sender, EventArgs e)
        {
            addDoc(33 );
        }

        private void toolStripButton12_Click(object sender, EventArgs e)
        {
            addDoc(22);
        }

        private void toolStripButton13_Click(object sender, EventArgs e)
        {
            addDoc(5);
        }

        private void toolStripButton11_Click(object sender, EventArgs e)
        {
            addDoc(5 );
        }

        private void toolStripButton3_Click_1(object sender, EventArgs e)
        {
            addDoc(5);
        }

        private void toolStripButton3_Click_2(object sender, EventArgs e)
        {
            addDoc(5);

        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
           
        }
        
        
        
        
        private void setForeColorButtons(raw.typeDoc tdo)
        {
            toolStripMenuItem2.ForeColor = Color.Gainsboro;
            toolStripMenuItem4.ForeColor = Color.Gainsboro;
            toolStripMenuItem5.ForeColor = Color.Gainsboro;
            toolStripMenuItem6.ForeColor = Color.Gainsboro; 
            if (tdo == ETALON.raw.typeDoc.NKP)    { toolStripMenuItem2.ForeColor = Color.MediumSeaGreen;}
            else if (tdo == ETALON.raw.typeDoc.SFP) { toolStripMenuItem4.ForeColor = Color.SteelBlue; }
            else if (tdo == ETALON.raw.typeDoc.NKRS) { toolStripMenuItem5.ForeColor = Color.DarkOrange; }
            else if (tdo == ETALON.raw.typeDoc.NKR) { toolStripMenuItem6.ForeColor = Color.Chocolate; }

        }
        private void funSetMode(raw.typeDoc tdo, string stxto, Color col)
        {
            currentTypeDoc = tdo; q.put(param.sir_activTypeDoc_22, (int)tdo);  toolStripStatusLabel3.Text = stxto;   toolStripStatusLabel3.BackColor = col;
            setForeColorButtons(tdo);
            ////if (tdo == ETALON.raw.typeDoc.NKP)      { statusStrip1.BackColor = Color.DarkSeaGreen;   statusStrip2.BackColor = Color.DarkSeaGreen; }
            ////else if (tdo == ETALON.raw.typeDoc.SFP) { statusStrip1.BackColor = Color.LightSteelBlue; statusStrip2.BackColor = Color.LightSteelBlue; }
            ////else if (tdo == ETALON.raw.typeDoc.SFR) { statusStrip1.BackColor = Color.Khaki;          statusStrip2.BackColor = Color.Khaki; }
            ////else if (tdo == ETALON.raw.typeDoc.NKR) { statusStrip1.BackColor = Color.Goldenrod;      statusStrip2.BackColor = Color.Goldenrod; }
            loadDoc();
        }
        private void setModeTypeDoc(raw.typeDoc mo)
        {
            if (mo == ETALON.raw.typeDoc.NKP)        { funSetMode(ETALON.raw.typeDoc.NKP, "Тип : Нк Приход", Color.MediumSeaGreen);}
            else if (mo == ETALON.raw.typeDoc.SFP)   { funSetMode(ETALON.raw.typeDoc.SFP, "Тип : Сф Приход", Color.SteelBlue);     }
            else if (mo == ETALON.raw.typeDoc.NKRS)   { funSetMode(ETALON.raw.typeDoc.NKRS, "Тип : Сф Расход", Color.DarkOrange);    }
            else if (mo == ETALON.raw.typeDoc.NKR)   { funSetMode(ETALON.raw.typeDoc.NKR, "Тип : Нк Расход", Color.Chocolate);     }
            
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {

        }

        ////private void toolStripMenuItem9_Click(object sender, EventArgs e)
        ////{
        ////    setModeTypeDoc(ETALON.raw.typeDoc.NKP); 
        ////}

        ////private void toolStripMenuItem11_Click(object sender, EventArgs e)
        ////{
        ////    setModeTypeDoc(ETALON.raw.typeDoc.SFP);
        ////}

        ////private void toolStripMenuItem13_Click(object sender, EventArgs e)
        ////{
            
        ////}

        ////private void toolStripMenuItem15_Click(object sender, EventArgs e)
        ////{
           
        ////}

        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            setModeTypeDoc(ETALON.raw.typeDoc.NKP); 
        }

        private void toolStripMenuItem9_Click_1(object sender, EventArgs e)
        {
            setModeTypeDoc(ETALON.raw.typeDoc.SFP);
        }

        private void toolStripMenuItem8_Click_1(object sender, EventArgs e)
        {
            setModeTypeDoc(ETALON.raw.typeDoc.NKRS);
        }

        private void toolStripMenuItem21_Click(object sender, EventArgs e)
        {
            setModeTypeDoc(ETALON.raw.typeDoc.NKR);
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            editDoc();
        }

        private void toolStripMenuItem40_Click(object sender, EventArgs e)
        {
            //DataTable dt = q.pr.pro_listParam_read;
            //q.sh(dt);
            //q.pr.pro_listParam_save();
            ETALON.FormCreateFileParam f = new ETALON.FormCreateFileParam(); f.ShowDialog();

        }

        private void listView3_DoubleClick(object sender, EventArgs e)
        {
          //  editDoc(); 
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (currentTypeDoc == ETALON.raw.typeDoc.NKP)
            {
                addDocTd_SkladNk2();
            }
            else if (currentTypeDoc == ETALON.raw.typeDoc.NKR )
            {
                addDocTd();
            }
        }

        private void listView2_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            selectSir();
        }
        private void deleteSir()
        {
            //try
            //{
                //q.UpdateData(sql_SAED.DEL_plase(q.tk_int(param.zakaz_current_13), q.tk_int(param.zakaz_plase_current_15)));
                //listView1.SelectedItems[0].Remove();
            object oo = q.tk_readTagTd(param.sir_td_tag_29, ETALON.raw.typeTagTd.cod123_5 ); //h.m(oo);
                q.UpdateData(sql_doc.DEL_td(oo));
                listView2.SelectedItems[0].Remove();
            
                loadDocTd();

                setModeWAED(raw.modeWAED.delTd);
            //}
            //catch { }
        }
        private void selectSir()
        {
            try
            {
                //q.put(param.zakaz_td_cod123_19, listView2.SelectedItems[0].Tag);
                //setModeWAED(ETALON.ZAKAZ.modeWAED.deleteOrEdit_Prod);
                q.put(param.sir_td_tag_29 , listView2.SelectedItems[0].Tag);
                setModeWAED(raw.modeWAED.afterSelectTd);
            }
            catch { }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            deleteSir();

        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            setModeTypeDoc(ETALON.raw.typeDoc.NKP);
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            setModeTypeDoc(ETALON.raw.typeDoc.SFP);
        }

        private void toolStripMenuItem5_Click_1(object sender, EventArgs e)
        {
            setModeTypeDoc(ETALON.raw.typeDoc.NKRS);
        }

        private void toolStripMenuItem6_Click_1(object sender, EventArgs e)
        {
            setModeTypeDoc(ETALON.raw.typeDoc.NKR);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton7_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        void IsHereDoc(int coddo)
        {
 
        }

        void selectOnIndex(int indxo)
        {
            listView3.Items[indxo].Selected = true;

            string stext = listView3.Items[indxo].Text;
            string stag = listView3.Items[indxo].Tag.ToString();
           
            string stag_doc = functionService.tagWork.readTAG(stag, (int)raw.typeTagRd.codd001_0);
            q.put(param.sir_current_doc_20, stag_doc);
            string stag_doc_cod123 = functionService.tagWork.readTAG(stag, (int)raw.typeTagRd.cod123_3 );
            q.put(param.sir_current_doc_cod123_21, stag_doc_cod123);
            q.put(param.sir_tag_rd_24, stag);
            // fs.shorts.m(stext);
            setStatus(stext); loadDocTd();
            setModeWAED(ETALON.raw.modeWAED.afterSelectRd);

        }

        private void toolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            
            //string stext = listView3.SelectedItems[0].Text;
            //string stag = listView3.SelectedItems[0].Tag.ToString(); 
        }

        private void toolStripMenuItem8Click_2()
        {
            object iCodd = q.tk_readTagRd(param.sir_tag_rd_24, ETALON.raw.typeTagRd.codd_5);
            functionService.prm_city plpc = new functionService.prm_city();
            //--------------------------------------
            td = q.selectAny(sql_doc.td_nakl(iCodd));
            //--------------------------------------
            //GRID.FormGRID gr = new GRID.FormGRID(td);
            //gr.ShowDialog();
            Report rep = new Report("Nakladnaya", td, string.Concat("Накладная № ", tgw.readTAG(plpc.tk((int)param.sir_tag_rd_24), 0), "/", tgw.readTAG(plpc.tk((int)param.sir_tag_rd_24), 1), " от ", da.cod_to_date(tgw.readTAG(plpc.tk((int)param.sir_tag_rd_24), 4)), "\n\n"), string.Concat("\n\n                       Итого наименований - ", td.Compute("Count(name)", "name <> '0'"), " \n\n                       Отпустил _____________________________________________________ \n\n                       Получил _____________________________________________________"), "50;300;100;100;100;0;0", "№пп;Наименование;Кол-во1;Кол-во2;Итог Кол-во;col;col");
            rep.ShowReport();

        }

        private void toolStripMenuItem8_Click_2(object sender, EventArgs e)
        {
            toolStripMenuItem8Click_2();
            #region oldPrint
            //#region Формирование входной таблицы для накладной
            //DataTable dt = new DataTable();

            //dt.Columns.Add("tovName", typeof(string));
            //dt.Columns.Add("kol", typeof(double));

            //DataRow row = dt.NewRow();
            //row["tovName"] = "Товар1 С супер длинным названием таким вот длинннннным";
            //row["kol"] = "10";
            //dt.Rows.Add(row);

            //row = dt.NewRow();
            //row["tovName"] = "Товар2 длинным названием";
            //row["kol"] = "3";
            //dt.Rows.Add(row);

            //row = dt.NewRow();
            //row["tovName"] = "Товар3";
            //row["kol"] = "11";
            //dt.Rows.Add(row);
            //#endregion

            //fr.OpName = vv.o_l_0_name ;//q.tk_name(param.o_l_0);
            //fr.Psw = "AERF";
            //fr.DevName = "COM1";
            //fr.FlagOem = 1;

            //fr.FROpenDLL();
            //fr.FRFDocPrintDataTable(dt, "Накладная N002 от 17.12.2010", "Выдал:__________________; ;Получил:____________________");
            ////fr.FRFDocPrintNakl(dt_nakl, "Накладная N002 от 17.12.2010", "Получил:____________________");
            //fr.FRCloseDLL();
#endregion
        }

        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void toolStripButton5_Click_2(object sender, EventArgs e)
        {

        }
        //private bool lookDir(string fileName)
        //{
        //    bool IsHere = false;
        //    DirectoryInfo di = new DirectoryInfo("PLP");
        //    FileInfo[] fai = di.GetFiles();
        //    foreach (FileInfo fi in fai)
        //    {
        //        if (fileName == fi.Name.Replace(".xml", "")) { IsHere = true; break; }
        //    }
        //    return IsHere;
        //}
    }
    
}
